home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / pascalt1.zip / CHAP2.TXT < prev    next >
Text File  |  1988-01-15  |  17KB  |  389 lines

  1.  
  2.                    CHAPTER 2 - Getting started in Pascal
  3.  
  4.  
  5.                          YOUR FIRST PASCAL PROGRAM
  6.  
  7.              Lets get right into a program that really does  nothing
  8.         but  is an example of the most trivial Pascal program.  Load
  9.         Turbo  Pascal,  select TRIVIAL as a Work  file,  and  select
  10.         Edit.   This  assumes  that  you  have  been  successful  in
  11.         learning  how  to use the TURBO Pascal system.  If  you  are
  12.         using  TURBO Pascal 4.0, you will need to  load  TRIVIAL.PAS
  13.         from the File menu.
  14.  
  15.              You  should  now have the most trivial  Pascal  program
  16.         possible  on  your display, and we can take a look  at  each
  17.         part to define what it does.
  18.  
  19.              The  first  line  is required in  the  standard  Pascal
  20.         definition and is the program name which can be any name you
  21.         like,  as  long as it follows the rules  for  an  identifier
  22.         given  in  the  next  paragraph.  It  can  have  no  blanks,
  23.         otherwise  it would be considered as two words and it  would
  24.         confuse the compiler.  The first word "program" is the first
  25.         of  the  reserved  words mentioned earlier  and  it  is  the
  26.         indicator  to the Pascal compiler that this is the  name  of
  27.         the  program.  Notice that the line ends with  a  semicolon.
  28.         Pascal  uses  the semicolon as the statement  separator  and
  29.         although all statements do not actually end in a  semicolon,
  30.         most  do,  and use of the semicolon will clear up  later  in
  31.         your  mind.
  32.  
  33.              TURBO Pascal version 3.0 does not require the "program"
  34.         statement, but to remain compatible with standard Pascal, it
  35.         will simply ignore the entire statement.  I like to  include
  36.         a program name both to keep me thinking in standard  Pascal,
  37.         and  to add a little more indication of the purpose of  each
  38.         program.
  39.  
  40.                           WHAT IS AN IDENTIFIER?
  41.  
  42.              All identifiers, including program name, procedure  and
  43.         function names, type definitions, and constant and  variable
  44.         names,  will  start with an alphabetical  character  and  be
  45.         composed  of  any  combination  of  alphabetic  and  numeric
  46.         characters  with  no embedded blanks.  Upper or  lower  case
  47.         alphabetic  characters are not significant and may be  mixed
  48.         at  will.   (If you find this definition confusing  at  this
  49.         point,  don't worry about it, it will be clear later but  it
  50.         must  be defined early).  The standard definition of  Pascal
  51.         requires that any implementation (i.e. any compiler  written
  52.         by  some  company)  must use at least 8  characters  of  the
  53.         identifier  as  significant  and may  ignore  the  remaining
  54.         characters  if more are used.  Most implementations use  far
  55.  
  56.  
  57.  
  58.                                  Page 7
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                    CHAPTER 2 - Getting started in Pascal
  69.  
  70.  
  71.         more than 8. TURBO Pascal uses at least 63 characters in  an
  72.         identifier as being significant.
  73.  
  74.              Standard Pascal does not allow the use of underlines in
  75.         an  identifier but most implementations of Pascal allow  its
  76.         use after the first character.  Both TURBO Pascal  compilers
  77.         allow the use of the underline as an allowable character  in
  78.         an  identifier,  so it will be freely used  throughout  this
  79.         tutorial.   The  underline  is  used  in  the  program  name
  80.         "Puppy_Dog" which should be on your display at this time.
  81.  
  82.              Returning  to the example program, the next line  is  a
  83.         blank  line which is ignored by all Pascal compilers.   More
  84.         will be said about that at the end of this chapter.
  85.  
  86.                             NOW FOR THE PROGRAM
  87.  
  88.              The next two lines comprise the actual Pascal  program,
  89.         which  in  this  case does absolutely  nothing.   It  is  an
  90.         illustration  of the minimum Pascal program.  The two  words
  91.         "begin"  and "end" are the next two reserved words  we  will
  92.         consider.   Any  logical  grouping of  Pascal  code  can  be
  93.         isolated  by  bracketing  it with  the  two  reserved  words
  94.         "begin"  and "end".  You will use this construct  repeatedly
  95.         as  you  write  Pascal  code  so it  is  well  to  learn  it
  96.         thoroughly.   Code to be executed by conditional jumps  will
  97.         be  bracketed  by "begin" and "end", as will code  within  a
  98.         loop, and code contained within a subroutine (although  they
  99.         are called "procedures" in Pascal), and in many other  ways.
  100.         In  the present program, the "begin" and "end" are  used  to
  101.         bracket the main program and every Pascal program will  have
  102.         the main program bracketed in this manner.  Because there is
  103.         nothing to do in this program, there are no statements.
  104.  
  105.              Finally,  although it could be very easily  overlooked,
  106.         there  is one more very important part of the  program,  the
  107.         period  following  "end".  The period is the signal  to  the
  108.         compiler  that  it  has reached the end  of  the  executable
  109.         statements  and  is  therefore  finished  compiling.   Every
  110.         Pascal program will have one, and only one period in it  and
  111.         that  one period will be at the end of the program.  I  must
  112.         qualify that statement in this regard, a period can be  used
  113.         in  comments, and in text to be output.  In fact  there  are
  114.         some  data  formats that require using a period as  part  of
  115.         their structure.  The statement is true however, that  there
  116.         is  only  one  period in the executable  part  of  a  Pascal
  117.         program.   Think  of a Pascal program as one  long  sentence
  118.         with one period at the end.
  119.  
  120.              That  should  pretty well describe our  first  program.
  121.         Now  it  is time to compile and run it.  To do so  you  must
  122.  
  123.  
  124.                                  Page 8
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                    CHAPTER 2 - Getting started in Pascal
  135.  
  136.  
  137.         exit the editor using Ctrl-K-D, unless you modified the exit
  138.         command.
  139.  
  140.              Compile the program, and run it to observe the  result.
  141.         Since  this  program  doesn't do anything, it  is  not  very
  142.         interesting, so let's get one that does something.
  143.  
  144.                        A PROGRAM THAT DOES SOMETHING
  145.  
  146.              Load  the  Pascal program WRITESM and view it  on  your
  147.         monitor.   The filename is sort of cryptic for "Write  Some"
  148.         and  it  will  give a little output  to  the  monitor.   The
  149.         program  name  is "Kitty_Cat" which says nothing  about  the
  150.         program  itself  but can be any identifier  we  choose.   We
  151.         still have the begin and end to define the main program area
  152.         followed by the period.  However, now we have two additional
  153.         statements  between  the  begin and  end.   "Writeln"  is  a
  154.         special word and it is probably not surprising that it means
  155.         to write a line of data somewhere.  Without a modifier,  (to
  156.         be  explained  in due time), it will write  to  the  default
  157.         device  which,  in the case of our IBM  compatible,  is  the
  158.         video display.  The data within the parentheses is the  data
  159.         to  be  output to the display and although  there  are  many
  160.         possibilities  of  display  information,  we  will  restrict
  161.         ourselves  to  the simplest for the time  being.   Any  data
  162.         between   apostrophes   will  simply  be  output   as   text
  163.         information.
  164.  
  165.              The  special word "Writeln" is not a reserved word  but
  166.         is  defined by the system to do a very special job for  you,
  167.         namely  to output a line of data to the monitor.  It is,  in
  168.         fact,  a procedure supplied for you by the writers of  TURBO
  169.         Pascal  as  a programming aid for you.  You can, if  you  so
  170.         desire, use this name for some other purpose in your program
  171.         but  doing so will not allow you to use the standard  output
  172.         procedure.   It will then be up to you to somehow  get  your
  173.         data out of the program.
  174.  
  175.              Note carefully that some words are reserved and  cannot
  176.         be  redefined and used for some other purpose, and some  are
  177.         special since they can be redefined.  You will probably  not
  178.         want to redefine any of the special words for a long time so
  179.         simply use them as tools.
  180.  
  181.              Notice the semicolon at the end of line 4.  This is the
  182.         statement  separator  referred to earlier and  tells  Pascal
  183.         that  this  line is complete as it stands, nothing  more  is
  184.         coming that could be considered part of this statement.  The
  185.         next statement, in line 5, is another statement that will be
  186.         executed  sequentially  following the statement in  line  4.
  187.         This  program  will output the two lines of text  and  stop.
  188.  
  189.  
  190.                                  Page 9
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                    CHAPTER 2 - Getting started in Pascal
  201.  
  202.  
  203.         Now it is time to go try it.  Exit the editor, then  compile
  204.         and run the program.
  205.  
  206.              You  should  get the two lines of text  output  to  the
  207.         video display every time you run it.  When you grow bored of
  208.         running WRITESM lets go on to another example.
  209.  
  210.                      ANOTHER PROGRAM WITH MORE OUTPUT
  211.  
  212.              Load  and  edit WRITEMR.  This new  program  has  three
  213.         lines  of  output but the first two  are  different  because
  214.         another  special  word is introduced to  us,  namely  Write.
  215.         Write  causes  the  text to be output in  exactly  the  same
  216.         manner  as  Writeln,  but Write does not  cause  a  carriage
  217.         return.   Writeln  causes  its output  to  take  place  then
  218.         returns  the "carriage" to the first character of  the  next
  219.         line.  The end result is that all three of the lines will be
  220.         output  on  the same line when the program is  run.   Notice
  221.         that  there is a blank at the end of each of the  first  two
  222.         lines  so  that  the formatting will look  nice.   Exit  the
  223.         editor now and try the new program.
  224.  
  225.              Now  might be a good time for you to return to  editing
  226.         WRITEMR and add a few more output commands to see if they do
  227.         what  you think they should do.  When you tire of  that,  we
  228.         will go on to the next file and learn about comments  within
  229.         a Pascal program.
  230.  
  231.                       ADDING COMMENTS IN THE PROGRAM
  232.  
  233.              The file named PASCOMS is similar to the others  except
  234.         that  comments  have  been added to  illustrate  their  use.
  235.         Pascal  defines  comments as anything between (* and  *)  or
  236.         anything  between  {  and }.   Originally  only  the  wiggly
  237.         brackets  were defined but since many keyboards didn't  have
  238.         them available, the parenthesis star combination was defined
  239.         as  an  extension and is universal by now, so  you  can  use
  240.         either.   Most of the comments are self  explanatory  except
  241.         for  the  one within the code.  Since comments can  go  from
  242.         line  to line, the two lines that would print  "send  money"
  243.         are  not Pascal code but are commented out.   Try  compiling
  244.         and running this program, then edit the comments out so that
  245.         "send money" is printed also.
  246.  
  247.              A  fine  point should be mentioned here.   Even  though
  248.         some compilers allow comments to start with (* and end  with
  249.         },  or  to  start with { and end with *), it  is  very  poor
  250.         programming  practice and should be discouraged.   The  ANSI
  251.         Pascal standard allows such usage but TURBO Pascal does  not
  252.         allow this funny use of comment delimiters.
  253.  
  254.  
  255.  
  256.                                  Page 10
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.                    CHAPTER 2 - Getting started in Pascal
  267.  
  268.  
  269.              TURBO Pascal does not allow you to nest comments  using
  270.         the  same delimiters but it does allow you to nest one  type
  271.         within  the other.  This could be used as a  debugging  aid.
  272.         If  you generally use the (* and *) for comments, you  could
  273.         use  the  { and } in TURBO Pascal to comment out  an  entire
  274.         section  of  code  during debugging even if  it  had  a  few
  275.         comments  in it.  This is a trick you should  remember  when
  276.         you reach the point of writing programs of significant size.
  277.  
  278.              When you have successfully modified and run the program
  279.         with  comments,  we will go on to  explain  good  formatting
  280.         practice  and  how  Pascal actually  searches  through  your
  281.         source file (Pascal program) for its executable statements.
  282.  
  283.              It  should be mentioned that the program  PASCOMS  does
  284.         not indicate good commenting style.  The program is meant to
  285.         illustrate where and how comments can be used and looks very
  286.         choppy  and unorganized.  Further examples  will  illustrate
  287.         good  use  of comments to you as you progress  through  this
  288.         tutorial.
  289.  
  290.                          GOOD FORMATTING PRACTICE
  291.  
  292.              Observe  GOODFORM to see an example of good  formatting
  293.         style.   It is important to note that Pascal doesn't give  a
  294.         hoot  where you put carriage returns or how many blanks  you
  295.         put  in when a blank is called for as a  delimiter.   Pascal
  296.         only  uses  the combination of reserved  words  and  end-of-
  297.         statement  semicolons to determine the logical structure  of
  298.         the  program.   Since  we  have  really  only  covered   two
  299.         executable  statements,  I have used them to  build  a  nice
  300.         looking  program that can be easily understood at a  glance.
  301.         Compile and run this program to see that it really does what
  302.         you think it should do.
  303.  
  304.                        VERY POOR FORMATTING PRACTICE
  305.  
  306.              Edit  UGLYFORM  now  to  see  an  example  of  terrible
  307.         formatting style.  It is not really apparent at a glance but
  308.         the  program you are looking at is exactly the same  program
  309.         as  the last one.  Pascal doesn't care which one you ask  it
  310.         to  run because to Pascal, they are identical.  To you  they
  311.         are  considerably different, and the second one would  be  a
  312.         mess to try to modify or maintain sometime in the future.
  313.  
  314.              UGLYFORM should be a good indication to you that Pascal
  315.         doesn't  care about programming style or form.  Pascal  only
  316.         cares  about  the structure, including  reserved  words  and
  317.         delimiters such as blanks and semicolons.  Carriage  returns
  318.         are  completely  ignored as are extra blanks.  You  can  put
  319.         extra blanks nearly anywhere except within reserved words or
  320.  
  321.  
  322.                                  Page 11
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.                    CHAPTER 2 - Getting started in Pascal
  333.  
  334.  
  335.         variable   names.    You  should  pay  some   attention   to
  336.         programming  style but don't get too worried about  it  yet.
  337.         As  time  goes  by you will develop  a  style  of  statement
  338.         indentation, adding blank lines for clarity, and a method of
  339.         adding  clear comments to Pascal source code.  Programs  are
  340.         available to read your source code, and put it in a "pretty"
  341.         format, but that is not important now.
  342.  
  343.              Not  only  is the form of the  program  important,  the
  344.         names used for variables can be very helpful or hindering as
  345.         we will see in the next chapter.  Feel free to  move  things
  346.         around and modify the format of any of the programs we  have
  347.         covered  so  far and when you are ready, we  will  start  on
  348.         variables in the next chapter.
  349.  
  350.              Be sure you compile and run UGLYFORM.
  351.  
  352.                            PROGRAMMING EXERCISES
  353.  
  354.         1.  Write  a  program that displays your name on  the  video
  355.             monitor.
  356.  
  357.         2.  Modify  your program to display your name and address on
  358.             one  line,  then  modify it by changing the  Write's  to
  359.             Writeln's so that the name and address are on  different
  360.             lines.
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.                                  Page 12
  389.